package com.example.controller;

import com.example.common.Result;
import com.example.entity.Comment;
import com.example.entity.Written;
import com.example.service.CommentService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @GetMapping("/selectAll")
    public Result selectAll(Comment comment) {
        List<Comment> commentList = commentService.selectAll(comment);
        return Result.success(commentList);
    }

    // 创建评论
    @PostMapping("/createComment")
    public Result createComment(@RequestBody Comment comment) {
        commentService.createComment(comment);
        return Result.success(comment);
    }

    //删除评论
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {  //@PathVariable 接收前端传来的路径参数
        commentService.deleteById(id);
        return Result.success();
    }

    // 获取评论树
    @GetMapping("/selectByWrittenId/{writtenId}")
    public Result selectByWrittenId(@PathVariable Integer writtenId) {
        List<Comment> comment = commentService.getCommentTree(writtenId);
        return Result.success(comment);
    }

    //根据用户id查找
    @GetMapping("/selectByUserId/{userId}")
    public Result selectByUserId(@PathVariable Integer userId) {
        List<Comment> comment = commentService.selectByUserId(userId);
        return Result.success(comment);
    }

    //分页查询
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Comment comment) {
        PageInfo<Comment> pageInfo = commentService.selectPage(pageNum, pageSize, comment);
        System.out.println("分页结果：" + pageInfo); // 调试日志
        return Result.success(pageInfo);
    }
}

